package com.maori.languageapp.curatesongs;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.maori.languageapp.R;
import com.maori.languageapp.consts.ImageLoader;
import com.maori.languageapp.consts.UrlContant;
import com.maori.languageapp.curate.modifynodes.Maori_Curate_Reject_Reson_Submit;
import com.maori.languageapp.curatestory.Maori_Curate_Story_Details;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Maori_Curate_Song_ImageList_Adapter extends BaseAdapter {
	private static final String TAG = null;
	// Declare Variables
	Context context;
	LayoutInflater inflater;
	ArrayList<HashMap<String, String>> data;
	ImageLoader imageLoader;
	String IMAGENID,IMAGENM_ID,result;
	Button curate_approve,curate_reject,curate_modify;
	HttpParams httpParameters = new BasicHttpParams();
	int timeoutConnection = 5000;
	InputStream is = null;
	StringBuilder sb = null;
	JSONArray jsonarray;
	HashMap<String, String> resultp = new HashMap<String, String>();
	String saveImageNodeApprove = UrlContant.saveImageNodeApprove;
	String NodeApproveReject = "0";
	String NodeApproveRejectMedia = "0";
	String Image_Active_A,Image_Active_B,ImageNMUser;
	TextView first_name,last_name;
	ImageView flag,userimage;
	
	public Maori_Curate_Song_ImageList_Adapter(Context context,
			ArrayList<HashMap<String, String>> arraylist) {
		this.context = context;
		data = arraylist;
		imageLoader = new ImageLoader(context);

	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// Declare Variables
		

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View itemViewImages = inflater.inflate(R.layout.languageapp_curate_image,
				parent, false);
		
		// Get the position from the results
		
		resultp = data.get(position);
		// Locate the ImageView in listview_item.xml
		first_name = (TextView) itemViewImages.findViewById(R.id.first_name);
		last_name = (TextView) itemViewImages.findViewById(R.id.last_name);
		flag = (ImageView) itemViewImages.findViewById(R.id.ImageView_example);
		userimage = (ImageView) itemViewImages.findViewById(R.id.userimggriditem);
		
		curate_approve = (Button) itemViewImages.findViewById(R.id.curate_approve);
		curate_reject = (Button) itemViewImages.findViewById(R.id.curate_reject);
		curate_modify = (Button) itemViewImages.findViewById(R.id.curate_modify);
		// Capture position and set results to the ImageView
		// Passes flag images URL into ImageLoader.class to download and cache
		// images
		imageLoader.DisplayImage(resultp.get(Maori_Curate_Song_Details.IMAGEM_Source),flag);
		imageLoader.DisplayImage(resultp.get(Maori_Curate_Song_Details.IMAGEMID),userimage);
		first_name.setText(resultp.get(Maori_Curate_Song_Details.IMAGEfirst_name));
		last_name.setText(resultp.get(Maori_Curate_Song_Details.IMAGElast_name));
		
		
		Image_Active_A = resultp.get(Maori_Curate_Song_Details.Image_Active_A);
		Image_Active_B = resultp.get(Maori_Curate_Song_Details.Image_Active_B);
		ImageNMUser = resultp.get(Maori_Curate_Song_Details.ImageNMUser);
		
		if (Image_Active_A.equals("1")) {
			curate_approve.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_tick, 0, 0, 0);
		} else if (Image_Active_A.equals("3")) {
			curate_reject.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_cross, 0, 0, 0);
		} else if (Image_Active_A.equals("4")) {
			curate_modify.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_pencil, 0, 0, 0);
		}
		
		
		curate_approve.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				IMAGENM_ID = resultp.get(Maori_Curate_Song_Details.IMAGENM_ID);
				IMAGENID = resultp.get(Maori_Curate_Song_Details.IMAGENID);
				NodeApproveReject = "1"; //
				NodeApproveRejectMedia = "1"; //
				Log.e("PRINT 1", " :" + resultp.get(Maori_Curate_Song_Details.IMAGENID));
				Log.e("PRINT 1", " :" + resultp.get(Maori_Curate_Song_Details.IMAGEUID));
				if (!Image_Active_A.equals("1")) {
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.b_tick, 0, 0, 0);
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_cross, 0, 0, 0);
					curate_modify.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_pencil, 0, 0, 0);
				} else {
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_tick, 0, 0, 0);
				}
				new sendToApproval().execute();
			}
		});
		
		curate_reject.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (!Image_Active_A.equals("3")) {
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.b_cross, 0, 0, 0);
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_tick, 0, 0, 0);
					curate_modify.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_pencil, 0, 0, 0);
				} else {
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_cross, 0, 0, 0);
				}
				Intent intent = new Intent(context,Maori_Curate_Reject_Reson_Submit.class);
				intent.putExtra("IMAGENM_ID",resultp.get(Maori_Curate_Song_Details.IMAGENM_ID));
				intent.putExtra("IMAGENID",resultp.get(Maori_Curate_Song_Details.IMAGENID));
				intent.putExtra("NodeApproveReject", "3");
				intent.putExtra("NodeApproveRejectMedia", "1");
				context.startActivity(intent);
			}
		});
		
		curate_modify.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				Log.e("PRINT 1", " :" + resultp.get(Maori_Curate_Song_Details.IMAGENID));
				Log.e("PRINT 1", " :" + resultp.get(Maori_Curate_Song_Details.IMAGEUID));
			}
		});
		// Capture button clicks on ListView items
		return itemViewImages;
	}
	/**************** Uploading Images Via FTP ************************/
	class sendToApproval extends AsyncTask<Void, Void, Void> {

		private final String TAG = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			ArrayList<NameValuePair> nameValuePairsMainW = new ArrayList<NameValuePair>();
			try {
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(saveImageNodeApprove);
				JSONObject json = new JSONObject();
				json.put("record0", IMAGENM_ID);
				json.put("record1", IMAGENID);
				json.put("record2", NodeApproveReject);
				json.put("record3", NodeApproveRejectMedia);
				
				JSONArray postjson = new JSONArray();
				postjson.put(json);
				Log.e("REQUEST ", "***:" + json.toString());
				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsMainW));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
					if (result != null) {
						Log.e("REQUEST :", "RESULT  :" + result);
						try {
							JSONObject jsonObj = new JSONObject(result);
							jsonarray = jsonObj.getJSONArray("posts");
							for (int i = 0; i < jsonarray.length(); i++) {
								JSONObject c = jsonarray.getJSONObject(i);
								// Get the result of rank
								Image_Active_A = c.getString("node_active_a");
								Image_Active_A = c.getString("node_active_b");
							}
						} catch (Exception e) {

						}
					}

				}

			} catch (Exception e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

		}
	}

}
